import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.*;

/**
 * 
 * @author Srinidhi Chaganty
 *
 */
public abstract class TransactionPanel extends JPanel{
	
	private JPanel totalPan;
	private JPanel transPan;
	private JPanel buyPan;
	private JPanel sellPan;
	private JPanel endPan;
	
	public final JPanel generatePanel(){
		totalPan = new JPanel(new BorderLayout());
		transPan = new JPanel();
		JPanel buttonsPan = new JPanel();
		JLabel intro = introLabel();
		final JButton buyButton = new JButton("Buy");
		buyButton.setBackground(Color.CYAN);
		buyButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == buyButton){
					transPan.setVisible(false);
					buyPan = new JPanel(new BorderLayout());
					buyPan.add(generateBuy(), BorderLayout.CENTER);
					final JButton back = new JButton("Back");
					back.setBackground(Color.BLUE);
					back.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							if(e.getSource() == back){
								buyPan.setVisible(false);
								transPan.setVisible(true);
							}
						}});
					buyPan.add(back, BorderLayout.PAGE_END);
					totalPan.add(buyPan);
				}
			}});
		final JButton sellButton = new JButton("Sell");
		sellButton.setBackground(Color.PINK);
		sellButton.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == sellButton){
					transPan.setVisible(false);
					sellPan = generateSell();
					final JButton back = new JButton("Back to Land Office");
					back.setBackground(Color.BLUE);
					back.addActionListener(new ActionListener(){
						@Override
						public void actionPerformed(ActionEvent e) {
							if(e.getSource() == back){
								sellPan.setVisible(false);
								transPan.setVisible(true);
							}
						}});
					sellPan.add(back, BorderLayout.PAGE_END);
					totalPan.add(sellPan);
					sellPan.setVisible(true);
				}
			}});
		//the two extended classes are located in the town
		final JButton back = new JButton("Back to Town");
		back.setBackground(Color.YELLOW);
		back.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				if(e.getSource() == back){
					transPan.setVisible(false);
					//town is a JPanel representation of the Town
					JPanel town;
					try {
						town = GameModel.getInstance().getMap().getTown().createTown();
						totalPan.add(town);
						town.setVisible(true);
					} catch (IOException e1) {
						e1.printStackTrace();
					}
				}
			}});
		transPan.add(intro);
		buttonsPan.add(buyButton);
		buttonsPan.add(sellButton);
		buttonsPan.add(back);
		transPan.add(buttonsPan);
		totalPan.add(transPan);
		return totalPan;
	}
	
	private final JPanel createIntro(){
		JPanel transPan = new JPanel(new BorderLayout());
		JPanel buttonsPan = new JPanel();
		JButton buyButton = new JButton("Buy");
		buyButton.setBackground(Color.CYAN);
		JButton sellButton = new JButton("Sell");
		sellButton.setBackground(Color.PINK);
		return transPan;
	}
	
	abstract JLabel introLabel();
	
	abstract JPanel generateBuy();
	
	abstract JPanel generateSell();
	
}
